package com.gy.handler;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.gy.domain.Lock;
import com.gy.domain.Room;
import com.gy.service.GetLocksService;
import com.gy.service.GetRoomsService;
import com.gy.service.OpenLocksService;

@Controller
@RequestMapping("OpenLockHandler")
public class OpenLockHandler {

	@Autowired
	private GetLocksService getLocksService;
	@Autowired
	private GetRoomsService getRoomsService;
	@Autowired
	private OpenLocksService openLocksService;

	List<Lock> locks = new ArrayList<Lock>();
	List<Room> rooms = new ArrayList<Room>();

	@ModelAttribute
	public void getlocks(Model model) {
		locks = getLocksService.getlocks();
		rooms = getRoomsService.getrooms();
		// System.out.println("baba");
		model.addAttribute("locks", locks);// request��
		model.addAttribute("rooms", rooms);// request��
	}

	//获取lock
	@RequestMapping(value = "getlock", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getlock() {

		String locksJson = JSON.toJSONString(locks); // List转json
		// System.out.println(locksJson);
		return locksJson;
	}

	//获取room
	@RequestMapping(value = "getroom", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getroom() {
		// JSONObject jsonObj = JSONObject.parseObject(resp);
		String roomsJson = JSON.toJSONString(rooms); // List转json
		// System.out.println(roomsJson);
		return roomsJson;
	}

	//获取公寓
	@RequestMapping(value = "getApartment", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getApartment() {
		Map<Integer, Integer> a = getRoomsService.getapartment();
		// JSONObject jsonObj = JSONObject.parseObject(resp);
		String apartmentJson = JSON.toJSONString(a);
		// System.out.println(apartmentJson);
		return apartmentJson;
	}

	@RequestMapping(value = "openLock")
	@ResponseBody
	public void openLock(@RequestParam("id") String id) {
		// System.out.println("ss");
		String arr[] = id.split("\\*");
		openLocksService.openlock(arr);
	}
}
